package com.cdhk.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.cdhk.service.CheckIfItHasBeenPublishedService;
import com.hustcad.plm.pdm.ec.module.dto.ECObjData;
import com.hustcad.plm.pdm.ec.module.vo.ECLinkObjDataCommonVO;
import com.hustcad.plm.pdm.ec.service.TyplmEcAgainstDataService;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowChangeRequestView;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowChgFormView;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObject;
import com.hustcad.plm.pdm.flowbusiness.model.entity.WorkFlowObjectPackage;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerView;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class CheckIfItHasBeenPublishedImpl implements CheckIfItHasBeenPublishedService {
    @Resource
    private TyplmWorkFlowContainerService typlmWorkFlowContainerService;
    @Resource
    private TyplmEcAgainstDataService typlmEcAgainstDataService;

    @Override
    public boolean checkIfItHasBeenPublished(BigInteger workFlowContainerId) {
        boolean isTrue =getFlowObjListByWorkFlowContainer(
                        workFlowContainerId);
        return isTrue;
    }

    public boolean getFlowObjListByWorkFlowContainer(BigInteger workFlowContainerId) {
        WorkFlowContainerView workFlowContainerView = typlmWorkFlowContainerService
                .getWorkFlowContainerDetailByPrimaryKey(
                        workFlowContainerId);
        if (Objects.isNull(workFlowContainerView)) {
            log.info("未找到 {} 对应的流程容器", workFlowContainerId);

        }
        return checkFlowObjLifeStatus(workFlowContainerView);
    }
    public boolean checkFlowObjLifeStatus(WorkFlowContainerView workFlowContainerView){
        boolean flag = true;
        if ((workFlowContainerView instanceof WorkFlowObjectPackage)) {
            log.info("当前流程属于普通流程");
            WorkFlowObjectPackage workFlowObjectPackage = (WorkFlowObjectPackage) workFlowContainerView;
            List<FlowObject> flowObjectList = workFlowObjectPackage.getFlowObjectList();
            if (CollUtil.isNotEmpty(flowObjectList)) {
                for (FlowObject flowObject : flowObjectList) {
                    String lifeCycleStageKey = flowObject.getLifeCycleStageKey();
                    if(!"RELEASED".equals(lifeCycleStageKey)){
                        flag = false;
                        break;
                    }
                }
            }
        } else if (workFlowContainerView instanceof FlowChgFormView) {
            log.info("当前流程属于变更流程/发布流程");
            FlowChgFormView flowChgFormView = (FlowChgFormView) workFlowContainerView;
            List<ECObjData> flowObjectList = flowChgFormView.getChgFormVO().getEcAffectVOS();
            if (CollUtil.isNotEmpty(flowObjectList)) {
                for (ECObjData ecObjData : flowObjectList) {
                    String lifecycleStageKey = ecObjData.getLifecycleStageKey();
                    if(!"RELEASED".equals(lifecycleStageKey)){
                        flag = false;
                        break;
                    }
                }
            }
        } else if (workFlowContainerView instanceof FlowChangeRequestView) {
            log.info("当前流程属于变更请求流程");
            List<ECLinkObjDataCommonVO> ecLinkObjDataCommonVOS = typlmEcAgainstDataService
                    .batchGetAffectedObjectListByOids(
                            Collections.singletonList(workFlowContainerView.getBaseObjectOid()),
                            workFlowContainerView.getBaseObjectOtype());
            if (CollUtil.isNotEmpty(ecLinkObjDataCommonVOS)) {
                for (ECLinkObjDataCommonVO ecLinkObjDataCommonVO : ecLinkObjDataCommonVOS) {
                    String lifeCycleState = ecLinkObjDataCommonVO.getLifeCycleState();
                    if(!"RELEASED".equals(lifeCycleState)){
                        flag = false;
                        break;
                    }
                }
            }
        }
        return flag;
    }
}
